package com.no1.musicmgr.service;


import com.no1.musicmgr.entity.User;
import com.no1.musicmgr.mapper.UserMapper;
import com.no1.musicmgr.util.DateUtil;
import com.no1.musicmgr.util.MD5Util;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Log4j
@Service
@Schema(description = "用户服务类")
public class UserService {
    @Autowired
    private UserMapper userMapper;


    @Schema(description = "检查用户名是否唯一")
    public boolean isYhmUnique(String user_name) {
        return userMapper.isYhmUnique(user_name) > 0;
    }

    @Schema(description = "检查用户名是否唯一")
    public boolean selectYhm(String user_name) {
        return userMapper.selectYhm(user_name);
    }

    @Schema(description = "注册用户")
    public boolean register(User user) {
//        String pwd = MD5Util.getEncrypCode(user.getPwd());
//        user.setPwd(pwd);
        return userMapper.register(user);
    }

    @Schema(description = "注册用户时的手机号和邮箱")
    public boolean registerAdd(User user) {
        return userMapper.registerAdd(user);
    }

    // 三种方式 用户名 手机号 邮箱
    @Schema(description = "用户登入")
    public User select(User user) {
        return userMapper.select(user);
    }

    @Schema(description = "用户查询")
    public ArrayList<User> list(User user) {
        return userMapper.list(user);
    }

    @Schema(description = "用户删除")
    public boolean deleteById(String user_id) {
        return userMapper.deleteById(user_id);
    }

    @Schema(description = "日期格式转换")
    public String formatDate(String input) {
        String res = null;
        if (input != null && !input.isEmpty()) {
            if (input.length() == 4) {
                if (Integer.parseInt(input) < 1232) {
                    res = DateUtil.formatDate(input);
                }
            } else {
                res = DateUtil.formatDate(input);
            }
        }
        return res;
    }


    @Schema(description = "修改用户名")
    public boolean updateYhm(User user) {
        return userMapper.updateYhm(user);
    }


    @Schema(description = "修改密码")
    public boolean updatePwd(User user) {
        return userMapper.updatePwd(user);
    }

    @Schema(description = "查找个人信息")
    public User selectme(String user_id) {
        return userMapper.selectme(user_id);
    }

    public boolean update(User user) {
        return userMapper.update(user);
    }

    @Schema(description = "用用户名查找个人信息")
    public User select1(String user_name) {
        return userMapper.select1(user_name);
    }

    @Schema(description = "根据用户id查找个人信息")
    public User selectById(User user) {
        try {
            return userMapper.selectById(user);
        }catch (Exception e){
            return new User();
        }
    }
}
